<?php

namespace app\admin\controller;

use app\base\controller\base;
use think\Db;
use think\Config;

class Api extends base
{
    public function index()
    {
        $name = request()->param('name');
        $a = config();
        dump($a);
        return json(array('status' => 1, 'msg' => '你好' . $name));
    }

    public function postLogin()
    {
        $username = request()->param('userName');
        $password = request()->param('password');
        return json(['code' => 200, 'msg' => '登录成功','data'=>['token'=>'1111']]);
    }

    public function getGet_info()
    {
        return json(['code' => 200, 'data' => ['name'=> 'super_admin',
                        'user_id'=> '11',
                        'access'=> ['super_admin', 'admin'],
                        'token'=>'super_admin',
                        'avator'=> 'https://file.iviewui.com/dist/a0e88e83800f138b94d2414621bd9704.png']]);
    }

    public function postLogout()
    {
        return json(['code'=>200,'data'=>[]]);
    }


    /**
     * @return \think\App
     */
    public function getTableList()
    {

        $indexJsonField = ['id'=>'编号','name'=>'姓名','description'=>'描述','status'=>'状态','sort_num'=>'排序'];

        $searchField = [];
        $vueField = [];
        foreach ($indexJsonField as $key=>$value){
            $searchField[] = $key;
            $vueField[] = array('title'=>$value,'key'=>$key,'tooltip'=>true,'editable'=>true);
        }
        $sortJsonField = ['name','description'];
        $table = request()->param('table');
        $list = Db::name($table)->field($searchField)->select();
        return json(['code'=>200,'data'=>$list,'sortData'=>$sortJsonField,'indexData'=>$vueField]);
    }


    public function postSaveEdit(){
        $table = $this->request->param('table');
        $name = $this->request->param('name');
        $value = $this->request->param('value');
        $id = $this->request->param('id');
        $rest = Db::name($table)->where('id',$id)->setField($name,$value);
        if($rest != false){
           return self::showJsonReturnCode(200,[],'更新成功');
        }else{
           return self::showJsonReturnCode(201,[],'数据更新失败');
        }
    }

    public function getreturnCodeTest(){
        dump( self::showReturnCode(1001));
        dump( self::showReturnCode(1002));
        dump( self::showJsonReturnCodeWithOutData(1003));
    }

    public function returnCode($code = '', $data = [], $msg = '')
    {
        return self::showReturnCode($code, $data, $msg); // TODO: Change the autogenerated stub
    }



}
